# CO.pm - PurlMUSH Command Object.
#
# A basic container for commands, their auth and code to run.

package Purl::Commands::CO;
use strict;

sub new {
    my ( $class, %arg ) = @_;
    bless {
        _name    => $arg{name}    || die("FAIL: missing name"),
        _subRef  => $arg{subRef}  || die("FAIL: missing command"),
        _authRef => $arg{authRef} || sub { return 1; },
    }, $class;
}

sub is_authorized {
    my ( $self, $dbref ) = @_;

    return $self->{_authRef}($dbref);
}    

sub run {
    my ( $self, %args ) = @_;

    if ( $self->is_authorized( $args{actor} ) ) {
        $self->{_subRef}(%args);
    }
    else {

        # The user is not allowed to do this command.
        Purl::comm::sendTextToPlayer(
            target => $args{actor},
            text  => "You are not allowed to run this command."
        );
    }
}

1;
